package jdbcConnect;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public  class Teacher extends User{
    String loginuser;
    String loginpwd;
    Connection conn = new JDBC().JDBCConnectvoid();
    Statement stmt = conn.createStatement();
    ResultSet rs = null;
    Scanner s=new Scanner(System.in);

    public Teacher(String loginuser, String loginpwd) throws SQLException {
        this.loginuser = loginuser;
        this.loginpwd = loginpwd;
    }
    public Teacher() throws SQLException {

    }
    public void Insert() throws SQLException {
        System.out.println("请输入学生的学号：");
        String TstuNo = s.nextLine();
        System.out.println("请输入学生的姓名：");
        String TstuName = s.nextLine();
        System.out.println("请输入学生的年龄：");
        int TstuAge = s.nextInt();
        System.out.println("请输入学生的性别：");
        String s1 = s.nextLine();
        String Tsex = s.nextLine();
        String TstuPwd = "123456";
        String inssql = "insert into student values('" + TstuNo + "','" + TstuName + "','" + TstuAge + "','" + Tsex + "','" + TstuPwd + "')";
        int tt = stmt.executeUpdate(inssql);
        if (tt != 0) System.out.println("添加成功！");
        //根据查询添加学生的信息
        String sql = "select * from student where StudentNo='" + TstuNo + "'";
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + "->" + rs.getString(2) + "->" +
                    rs.getString(3) + "->" + rs.getString(4) + "->" + rs.getString(5));
        }
    }
    public void  Delete() throws SQLException {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入需要删除学生的学号：");
        String DelStuNo = s.nextLine();
        System.out.println("请问你是否要进行删除操作？一经删除概不负责！");
        String DelAnswer = s.next();
        if (DelAnswer.equals("yes") || DelAnswer.equals("Yes")) {
            //进行删除操作
            String DelSql = "delete from student where StudentNo='" + DelStuNo + "'";
            int DelNum = stmt.executeUpdate(DelSql);
            if (DelNum != 0) {
                System.out.println("删除成功！");
            } else System.out.println("删除失败！");
            System.out.println("以下是删除后的表格数据:");
            String sql="select * from student";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                System.out.println(rs.getString(1) + "->" + rs.getString(2) + "->" + rs.getString(3) + "->" + rs.getString(4) + "->" + rs.getString(5));
            }
        }

    }
    //老师服务器端
    public void Serve() throws IOException {
        ServerSocket serverSocket = new ServerSocket(1087);
        String Ip = InetAddress.getLoopbackAddress().getHostAddress();
        System.out.println("以下是接收方的IP地址:" + Ip);
        System.out.println("接收方启动中...");
        System.out.println("===接收端启动成功===");
        int Time_out = 10000;

        //接收发送到此端口号的消息
        Socket socket = null;
        BufferedReader br = null;
        try {
            socket = serverSocket.accept();
            //设置延时
            socket.setSoTimeout(Time_out);
            //调用输入流，读入发送过来的数据
            InputStream input = socket.getInputStream();
            //通过键盘读入接收数据
            br = new BufferedReader(new InputStreamReader(input));
            System.out.println("此时开始计时！");
            int count = 0;
            boolean flag = true;
            do {
                String message = br.readLine();
                if(message==null){
                    System.out.println("服务端停止发送消息");
                    System.out.println("服务端即将退出...");
                    System.out.println("服务端已退出");
                    serverSocket.close();
                    br.close();
                    break;
                }else {
                    System.out.println(socket.getRemoteSocketAddress() + "接收的消息为:" + message);
                    count++;
                    System.out.println("当前已收到" + count + "条消息");
                    if (count > 1000) {
                        System.out.println("接收端已经到达最大峰值...\n即将关闭服务器");
                        //资源释放
                        serverSocket.close();
                        br.close();
                        System.out.println("接收端口已关闭");
                        flag = false;
                    }
                }
            } while (flag);
        } catch (IOException e) {
            System.out.println("超过"+Time_out/1000+"秒未收到消息\t接收端即将关闭...");
            System.out.println("接收端口已关闭");
            br.close();
            serverSocket.close();
        }
    }

}





